var t1 = "";
var dat;
var d;
function ticketsForm(){
    var  destination= document.getElementById("destination").value;
    var  date= document.getElementById("D").value;
    dat =date;
    console.log(destination);
    console.log(date);
    console.log(dat);
    if(destination=='--目的地--'||date==''){
        alert("请完成全部选择在搜索");
    }else{
        $.ajax({
            url:'/see',
            type:'post',
            sync:true,
            data:{
                "destination":destination,
                "date":date
            },
            dataType:'json',
            success:function (res){
                t1=res;
                console.log(t1);
                if (res.length > 0) {
                    console.log("长度大于0");
                    let Ticket = "";
                    let Ticket2 = "";
                    let g =0;
                    for (var i = 0; i < res.length; i++) {
                        if(res[i].classify==0){
                            //console.log(i);
                            g++;
                            Ticket += '第'+g+'种方案:<br><span>车号:' + res[i].car + '&nbsp;</span><span>出发地:' + res[i].departure + '&nbsp;</span><span>目的地:' + res[i].destination + '&nbsp;</span><span>出发时间:' + res[i].Htimea + '&nbsp;</span><span>到达时间:' + res[i].Htimeb + '&nbsp;</span><span>票数:' + res[i].poll + '&nbsp;</span><span>金额:' + res[i].money + '</span>' +'<input type=\"radio\" name=\"car\" id=\"car\" value=\"'+i+'\" />'+'<br>';
                        }else if(res[i].classify>0){
                            if(res[i].classify==1){
                                g++;
                                Ticket2 += '第'+g+'种方案:<br><span>车号:' + res[i].car + '&nbsp;</span><span>出发地:' + res[i].departure + '&nbsp;</span><span>目的地:' + res[i].destination + '&nbsp;</span><span>出发时间:' + res[i].Htimea + '&nbsp;</span><span>到达时间:' + res[i].Htimeb + '&nbsp;</span><span>票数:' + res[i].poll + '&nbsp;</span><span>金额:' + res[i].money + '</span><br>';
                            }else{
                                Ticket2 += '<span>车号:' + res[i].car + '&nbsp;</span><span>出发地:' + res[i].departure + '"&nbsp;</span><span>目的地:' + res[i].destination + '"&nbsp;</span><span>出发时间:' + res[i].Htimea + '"&nbsp;</span><span>到达时间:' + res[i].Htimeb + '"&nbsp;</span><span>票数:' + res[i].poll + '"&nbsp;</span><span>金额:' + res[i].money + '"&nbsp;</span><span>总金额:'+(res[i].money+res[i-1].money)+'"</span>'+'<input type=\"radio\" name=\"car\" id=\"car\" value=\"'+i+'\" />'+'<br>';
                            }
                        }
                    }
                    console.log(Ticket);
                    $('#main').html(Ticket);
                    $('#main2').html(Ticket2);
                }
            }
        });
    }

}
function establish(n) {
    d='';
    $.each($('input:radio'),function(){
        if(this.checked){
            d =$(this).val();
            console.log(d);
        }
    });
    if(d==''){
        alert("没有选择方案就提交");
    }else{
        var d1 = t1[d];
        var Tick="";
        if(d1.classify==0){
            //console.log(i);
            console.log("d1.car="+d1.car);

            $('#car1').val(d1.car);
            // $('#car1').text(d1.car);
            $('#car2').val(d1.car);
            $('#departure1').val(d1.departure);
            $('#departure2').val(d1.departure);
            $('#destination1').val(d1.destination);
            $('#destination2').val(d1.destination);
            $('#Date').val(dat);
            $('#Htimea1').val(d1.Htimea);
            $('#Htimea2').val(d1.Htimea);
            $('#Htimeb1').val(d1.Htimeb);
            $('#Htimeb2').val(d1.Htimeb);
            $('#polla').val(d1.poll);
            $('#pollb').val(d1.poll);
            $('#mon').val(d1.money);
            console.log("这类型是"+d1.classify);
            $('#classify').val(d1.classify);
            Tick += '<span>车号:' + d1.car + '&nbsp;</span><br><span>出发地:' + d1.departure + '&nbsp;</span><span>目的地:' + d1.destination + '&nbsp;</span><br><span>出发时间:' + d1.Htimea + '&nbsp;</span><span>到达时间:' + d1.Htimeb + '&nbsp;</span><br><span>票数:' + d1.poll + '&nbsp;</span><br><span>金额:' + d1.money + '</span><br>';
        }else if(d1.classify>0){
            var d2 = t1[d-1];
            if(d1.classify==2){
                $('#car1').val(d2.car);
                $('#car2').val(d1.car);
                $('#departure1').val(d2.departure);
                $('#departure2').val(d1.departure);
                $('#destination1').val(d2.destination);
                $('#destination2').val(d1.destination);
                $('#Date').val(dat);
                $('#Htimea1').val(d2.Htimea);
                $('#Htimea2').val(d1.Htimea);
                $('#Htimeb1').val(d2.Htimeb);
                $('#Htimeb2').val(d1.Htimeb);
                $('#polla').val(d2.poll);
                $('#pollb').val(d1.poll);
                $('#mon').val((d1.money+d2.money));
                $('#classify').val(d1.classify);
                Tick += '<span>第一趟车<br>车号:' + d2.car + '&nbsp;</span><span>出发地:' + d2.departure + '&nbsp;</span><span>目的地:' + d2.destination + '&nbsp;</span><br><span>出发时间:' + d2.Htimea + '&nbsp;</span><span>到达时间:' + d2.Htimeb + '&nbsp;</span><br><span>票数:' + d2.poll + '&nbsp;</span><span>金额:' + d2.money + '</span><br>';
                Tick += '<span>第二趟车<br>车号:' + d1.car + '&nbsp;</span><span>出发地:' + d1.departure + '&nbsp;</span><span>目的地:' + d1.destination + '&nbsp;</span><br><span>出发时间:' + d1.Htimea + '&nbsp;</span><span>到达时间:' + d1.Htimeb + '&nbsp;</span><br><span>票数:' + d1.poll + '&nbsp;</span><span>金额:' + d1.money + '&nbsp;</span><span>总金额:'+(d1.money+d2.money)+'</span><br>';
            }
        }
        $('#tic').html(Tick);
        var a;
        var div= document.getElementById('inputbox');
        div.style.display = n ? 'block' : 'none';     /* 点击按钮打开/关闭 对话框 */
        if (n) {
            a = n.parentNode.getElementsByTagName('a')[0];
            ipt = div.getElementsByTagName('input');
            //ipt[0].value = null;
        }
    }

}
